package biz.datalk.industrialland.admin.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
 * 避免在GET请求中使用! * ’ ( ) ; : @ & = + $ , / ? # [ ])等字符
 * 如果使用了其中的某些字符，请启用改配置项
 *
 * @author tarofang@163.com
 * @date 2019-11-09
 */
@Configuration
public class TomcatConfig {

    @ConditionalOnProperty(name = "datalk.tomcat.invalid.character.support", havingValue = "true", matchIfMissing = true)
    @Bean
    public ServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory fa = new TomcatServletWebServerFactory();
        fa.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
            connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
            connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
        });
        return fa;
    }
}
